package dbPhase.hypeerweb;


/**
 * StandardState is a state that represents a node in a Standard state.
 *
 * @see NodeState
 *
 * @pre singleton - StandardState
 *
 *
 */
public class StandardNodeState extends NodeState {

	static StandardNodeState singleton;

	private StandardNodeState() {
	}

	static StandardNodeState getSingleton() {
		if (singleton == null) {
			singleton = new StandardNodeState();
		}
		return singleton;
	}

	public static final int STATE_ID = 0;

	@Override
	public Node find(Node thisNode) {
		Node biggest = thisNode.getBiggestNeighbor();
		int biggestid = biggest.getWebId().getValue();
		if (thisNode.fold.webId.getValue() > biggestid) {
			biggestid = thisNode.fold.webId.getValue();
			biggest = thisNode.fold;
		}
		if (thisNode.inverseSurrogateFold.webId.getValue() > biggestid) {
			biggestid = thisNode.inverseSurrogateFold.webId.getValue();
			biggest = thisNode.inverseSurrogateFold;
		}
		if (thisNode.surrogateFold.webId.getValue() > biggestid) {
			biggestid = thisNode.surrogateFold.webId.getValue();
			biggest = thisNode.surrogateFold;
		}

		return biggest.nodeState.find(biggest);
	}

	@Override
	public Node search(int lb, int ub, Node thisNode ) {
		System.err.println("Search should never be called on a Standard node: ");
		System.err.println("Lower " + lb + " Upper " + ub + " This " + thisNode.webId.getValue());
		return null;
	}

	@Override
	public String toString() {
		return "Standard Node State";
	}

	@Override
	public int getStateId() {

		return STATE_ID;
	}

	@Override
	public Node findDP(Node thisNode) {
		Node biggest = thisNode.getBiggestNeighbor();
		int biggestid = biggest.getWebId().getValue();
		if (thisNode.fold.webId.getValue() > biggestid) {
			biggestid = thisNode.fold.webId.getValue();
			biggest = thisNode.fold;
		}
		if (thisNode.inverseSurrogateFold.webId.getValue() > biggestid) {
			biggestid = thisNode.inverseSurrogateFold.webId.getValue();
			biggest = thisNode.inverseSurrogateFold;
		}
		if (thisNode.surrogateFold.webId.getValue() > biggestid) {
			biggestid = thisNode.surrogateFold.webId.getValue();
			biggest = thisNode.surrogateFold;
		}

		return biggest.nodeState.findDP(biggest);
	}

	@Override
	public Node searchDP(int lb, int ub,Node thisNode) {
		System.err.println("Search should never be called on a Cap node: ");

		return Node.NULL_NODE;
	}

}
